package com.fingard.dsp.bank.directbank.cmb01;

import javax.sql.rowset.spi.XmlWriter;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IForeignCurrency;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqQryForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.RetAtsForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.detail.RetAtsForeignCurrPayDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CMB01ForeignCurrSinglePay extends CMB01Base implements IForeignCurrency
{
    public RetAtsForeignCurrSingle foreignCurrPay(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception
    {
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetDetail;
        if("CrossborderFCP".equals(reqForeignCurrSingle.reqHeader.transType) || "DomesticFCP".equals(reqForeignCurrSingle.reqHeader.transType) ||"OutsideFCP".equals(reqForeignCurrSingle.reqHeader.transType))
        {
            int Enum = -1;
            boolean IsSameBank = reqForeignCurrSingle.singleDetail.sameBnk.equals("1");
            boolean IsSameCountry = reqForeignCurrSingle.singleDetail.recCountryCode.equals("CHN");
            if( IsSameBank &&  IsSameCountry)
            {
                Enum = 2;
            }
            else if(IsSameBank && !IsSameCountry)
            {
                Enum = 1;
            }
            else if(!IsSameBank && IsSameCountry )
            {
                Enum = 3;
            }
            else if(!IsSameBank && !IsSameCountry )
            {
                Enum = 4;
            }
            tmpRetDetail = DealForeignPay(reqForeignCurrSingle,Enum);
        }
        else
        {
            tmpRetDetail = new RetAtsForeignCurrPayDetail();
            tmpRetDetail.respCode = "0";
            tmpRetDetail.respInfo = "无效的指令代码";
            tmpRetDetail.transState = PaymentState.FAIL_Payment;
            tmpRetDetail.payInfoCode = "E1301";
        }
        retAts.writeRetDetail(tmpRetDetail);
    return retAts;
}

    public RetAtsForeignCurrSingle qryForeignCurrPay(ReqQryForeignCurrSingle reqQryForeignCurrSingle) throws Exception
    {
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqQryForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetAtsDetail = new RetAtsForeignCurrPayDetail();
        tmpRetAtsDetail.respCode = "";
        if(tmpRetAtsDetail.respCode.length() == 0)
        {
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "NTRTOINF";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("NTRTOINFX");
            xmlWriter.writeElementString("REQNBR",reqQryForeignCurrSingle.askNumber);
            xmlWriter.writeEndDocument();
            try
            {
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if(tmpStrRet[0].length() == 0)
                {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null)
                    {
                        String tmpRespCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                        String tmpRetInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");

                        if("0".equals(tmpRespCode))
                        {
                            tmpRetAtsDetail.respCode = "0";
                            tmpRetAtsDetail.reqSeqID = reqQryForeignCurrSingle.askNumber;
                            tmpRetAtsDetail.payInfoCode = "";
                            tmpRetAtsDetail.transState = "";
                            tmpRetAtsDetail.respInfo = tmpRetInfo;
                        }
                        else
                        {
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = tmpRetInfo;
                        }
                    }
                    else
                    {
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = tmpSbMsg.toString() ;
                    }
                }
            }
            catch (Exception ex)
            {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = ex.getMessage();
                ex.printStackTrace();
            }
            retAts.writeRetDetail(tmpRetAtsDetail);
            return retAts;
        }
        return retAts;
    }

    public RetAtsForeignCurrPayDetail DealForeignPay(ReqForeignCurrSingle reqForeignCurrSingle,int Enum)
    {
        RetAtsForeignCurrPayDetail detail = new RetAtsForeignCurrPayDetail();
        detail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
        try{
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "NTRTOOP"+ Enum;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("NTRTOOPRX");
            writeCommonRequestBody(xmlWriter, reqForeignCurrSingle);
            if(Enum == 2)
            {
                xmlWriter.writeElementString("CNRCOD",getBankConfig().getMapKeyByValue("areaMapping",reqForeignCurrSingle.singleDetail.recCountryCode));
            }
            if(Enum == 3 || Enum == 4)
            {
                xmlWriter.writeElementString("CNRCOD",getBankConfig().getMapKeyByValue("areaMapping",reqForeignCurrSingle.singleDetail.recCountryCode));
                xmlWriter.writeElementString("RCBSWF",reqForeignCurrSingle.singleDetail.recSWIFTCODE);
                xmlWriter.writeElementString("RCBADR",reqForeignCurrSingle.singleDetail.recSWIFTCODE);
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("NTRTOOPTX");
            xmlWriter.writeElementString("BUSMOD","00002");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if(tmpStrRet[0].length() == 0)
            {
                StringBuilder GetBankRetInfo = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], GetBankRetInfo);
                if (tmpBkReader != null)
                {
                    detail.respCode = "0";
                    String tmpPayInfoCode = tmpBkReader.getFirstText("/CMBSDKPGK/NTRTARTNZ/ERRCOD");
                    if("SUC0000".equals(tmpPayInfoCode)){
                        tmpPayInfoCode = "FIN+S";
                    }
                    String tmpRespInfo = tmpBkReader.getFirstText(("/CMBSDKPGK/NTRTARTNZ/ERRTXT"));
                    detail.transState = tmpPayInfoCode;
                    detail.payInfoCode = tmpPayInfoCode;
                    detail.respInfo = tmpRespInfo;
                    detail.transAmt = reqForeignCurrSingle.singleDetail.payAmount;
                }
                else
                {
                    detail.respCode = "-1";
                    detail.respInfo = GetBankRetInfo.toString();
                }
            }
            else
            {
                detail = DealPayFailed(detail,tmpStrRet);
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return detail;
    }

    private void writeCommonRequestBody(XmlTextWriter xmlWriter,ReqForeignCurrSingle reqForeignCurrSingle) throws Exception
    {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
        String dateString = formatter.format(currentTime);
        xmlWriter.writeElementString("YURREF",dateString);
        xmlWriter.writeElementString("GRTFLG","Y");
        xmlWriter.writeElementString("ORTTYP","O");
        xmlWriter.writeElementString("RMTBBK",reqForeignCurrSingle.singleDetail.payAreaCode);
        xmlWriter.writeElementString("RMTACC",reqForeignCurrSingle.singleDetail.payAct);
        xmlWriter.writeElementString("RMTCCY","21");
        xmlWriter.writeElementString("RMTAMT",reqForeignCurrSingle.singleDetail.payAmount );
        xmlWriter.writeElementString("RCVACC",reqForeignCurrSingle.singleDetail.recAct);
        xmlWriter.writeElementString("RCVNAM", new String (reqForeignCurrSingle.singleDetail.recName.getBytes(),"GBK"));
        xmlWriter.writeElementString("RCVCNR",getBankConfig().getMapKeyByValue("areaMapping",reqForeignCurrSingle.singleDetail.recCountryCode));
        xmlWriter.writeElementString("CHGFOR","S");
        if("1".equals(reqForeignCurrSingle.singleDetail.isPrivate))
        {
            xmlWriter.writeElementString("CLTTYP","A");
            xmlWriter.writeElementString("INFTXT","OSA PAYMENT "+ reqForeignCurrSingle.singleDetail.purpose);
        }
        else
        {
            xmlWriter.writeElementString("CLTTYP", "P");
        }
    }

    private RetAtsForeignCurrPayDetail DealPayFailed(RetAtsForeignCurrPayDetail detail,String[] BankRet)
    {
        if(BankRet[0].equals(ConnState.FAIL))
        {
            detail.respCode = "0";
            detail.respInfo = BankRet[1];
            detail.transState = PaymentState.FAIL_Payment;
            detail.payInfoCode = BankRet[0];
        }
        else
        {
            detail.respCode = "-1";
            detail.respInfo = BankRet[1];
        }
        return detail;
    }
}
